﻿#region Copyright Info

// 
// Majesty of Omega - The next evolution of macro management space strategy
// 
// Copyright 2007-2009 André Claaßen
// 
// This file is protected by the terms and conditions of the
// Microsoft Community License (Ms-CL 1.1), a copy of which should
// have been distributed along with this software. If not,
// you may find the license information at the following URL:
// 
// http://www.microsoft.com/resources/sharedsource/licensingbasics/communitylicense.mspx

#endregion

#region

using System;
using System.Windows;
using DesignByContract;

#endregion

namespace Majesty_of_Omega.GUI.Pages.Game.Research.Parts
{
    /// <summary>
    /// Interaktionslogik für FundingLevel.xaml
    /// </summary>
    public partial class FundingLevelPart
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="FundingLevelPart"/> class.
        /// </summary>
        public FundingLevelPart()
        {
            InitializeComponent();
        }

        #region Dependency Property Percentage

        /// <summary>
        /// Dependency property Percentage
        /// </summary>
        public static readonly DependencyProperty PercentageProperty =
            DependencyProperty.Register("Percentage", typeof (double), typeof (FundingLevelPart), new UIPropertyMetadata(50.0, OnPercentageValueChanged));

        /// <summary>
        /// Change something, when the value of the dependency property Percentage
        /// </summary>
        /// <param name="d">instance of FundingLevel</param>
        /// <param name="e">the new and old values</param>
        private static void OnPercentageValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            double newValue = Convert.ToDouble(e.NewValue);
            FundingLevelPart levelPart = (FundingLevelPart) d;
            levelPart.ChangePercentageToReflectNewValue(newValue);
        }

        /// <summary>
        /// Gets or sets the percentage of the bar. The Bar adjusts its size to the value
        /// </summary>
        /// <value>The percentage.</value>
        public double Percentage
        {
            get { return (double)GetValue(PercentageProperty); }
            set { SetValue(PercentageProperty, value); }
        }

        #endregion

        /// <summary>
        /// Changes the percentage to the new value
        /// </summary>
        /// <param name="percentage">The percentage.</param>
        private void ChangePercentageToReflectNewValue(double percentage)
        {
            Require.IsTrue(percentage >= 0.0 && percentage <= 125.0, "The percentage value should lie in a range between 0 and 125");
            BlackBorderColumn.Width = new GridLength(125.0 - percentage, GridUnitType.Star);
            ProgressColumn.Width = new GridLength(percentage, GridUnitType.Star);
        }
    }
}